package myta.model;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * @author Gian Rishandy
 *
 */
public class DataRow implements List<Object>{
	
	/**
	 * Value of column 
	 */
	private List<DataColumn> columns;
	
	/**
	 * Value of row
	 */
	private List<Object> rows;

	public DataRow() {
		rows = new ArrayList<Object>();
	}
	
	public void addValue(Object value){
		rows.add(value);
	}
	
	public void removeValue(int index){
		rows.remove(index);
	}
	
	public void removeValue(String columnName){
		removeValue(getColumnIndex(columnName));
	}
	
	public void setValue(int index, Object value){
		rows.set(index, value);
	}
	
	public void setValue(String columnName, Object value){
		setValue(getColumnIndex(columnName), value);
	}
	
	public Object getObject(int index){
		return rows.get(index);
	}
	
	public Object getObject(String columnName){
		return getObject(getColumnIndex(columnName));
	}
	
	public String getString(int index){
		return getObject(index).toString();
	}
	
	public String getString(String columnName){
		return getObject(columnName).toString();
	}
	
	public int getInteger(int index){
		return Integer.valueOf(getString(index));
	}
	
	public int getInteger(String columnName){
		return Integer.valueOf(getString(columnName));
	}
	
	public double getDouble(int index){
		return Double.valueOf(getString(index));
	}
	
	public double getDouble(String columnName){
		return Double.valueOf(getString(columnName));
	}
	
	public Date getDate(int index) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.parse(getString(index));
	}
	
	public Date getDate(String columnName) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.parse(getString(columnName));
	}
	
	private int getColumnIndex(String columnName){
		if(columns != null){
			for(int i=0;i<columns.size();i++){
				DataColumn column = columns.get(i);
				String colName = column.getColumnName();
				if(colName.equalsIgnoreCase(columnName)){
					return i;
				}					
			}
		}
		return -1;
	}
	
	@Override
	public int size() {
		return rows.size();
	}

	@Override
	public boolean isEmpty() {
		return rows.isEmpty();
	}

	@Override
	public boolean contains(Object o) {
		return rows.contains(o);
	}

	@Override
	public Iterator<Object> iterator() {
		return rows.iterator();
	}

	@Override
	public Object[] toArray() {
		return rows.toArray();
	}

	@Override
	public <T> T[] toArray(T[] a) {
		return rows.toArray(a);
	}

	@Override
	public boolean add(Object e) {
		return rows.add(e);
	}

	@Override
	public boolean remove(Object o) {
		return rows.remove(o);
	}

	@Override
	public boolean containsAll(Collection<?> c) {
		return rows.containsAll(c);
	}

	@Override
	public boolean addAll(Collection<? extends Object> c) {
		return rows.addAll(c);
	}

	@Override
	public boolean addAll(int index, Collection<? extends Object> c) {
		return rows.addAll(index, c);
	}

	@Override
	public boolean removeAll(Collection<?> c) {
		return rows.removeAll(c);
	}

	@Override
	public boolean retainAll(Collection<?> c) {
		return rows.retainAll(c);
	}

	@Override
	public void clear() {
		rows.clear();
	}

	@Override
	public Object get(int index) {
		return rows.get(index);
	}

	@Override
	public Object set(int index, Object element) {
		return rows.set(index, element);
	}

	@Override
	public void add(int index, Object element) {
		rows.add(index, element);
	}

	@Override
	public Object remove(int index) {
		return rows.remove(index);
	}

	@Override
	public int indexOf(Object o) {
		return rows.indexOf(o);
	}

	@Override
	public int lastIndexOf(Object o) {
		return rows.lastIndexOf(o);
	}

	@Override
	public ListIterator<Object> listIterator() {
		return rows.listIterator();
	}

	@Override
	public ListIterator<Object> listIterator(int index) {
		return rows.listIterator(index);
	}

	@Override
	public List<Object> subList(int fromIndex, int toIndex) {
		return rows.subList(fromIndex, toIndex);
	}

	public List<DataColumn> getColumns() {
		return columns;
	}

	public void setColumns(List<DataColumn> columns) {
		this.columns = columns;
	}

	public List<Object> getRow() {
		return rows;
	}

	public void setRow(List<Object> row) {
		this.rows = row;
	}
}
